package shop;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import goods.GoodItem;

public class ShoppingCart {
    public List<GoodItem> item = new ArrayList<>();
    private static double totlePrice = 0;

    //展示商品条目
    public void showGoodsItem() {
        totlePrice = 0;
        System.out.println("******************购物车中的商品*****************************");
        System.out.println("|  ID   |  名称   |   单价  |   数量  |   总价   |");
        for (int i = 0; i < item.size(); i++) {
            System.out.println(item.get(i).toString());
            totlePrice += item.get(i).getPriceSum() * item.get(i).getNum();
        }
        if (item.size() == 0)
            totlePrice = 0;
        System.out.println("合计：" + totlePrice);
        System.out.println("**********************************************************");
    }

    //修改商品数量
    public void setNum(int id, int num) {
        //如果数量置为0,移出购物车
        if (num == 0)
            remove(id);
        else {
            for (int i = 0; i < item.size(); i++) {
                if (item.get(i).getId() == id) {
                    item.get(i).setNum(num);
                    break;
                }
            }
        }
    }

    //移出购物车
    public void remove(int id) {
        for (int i = 0; i < item.size(); i++) {
            if (item.get(i).getId() == id) {
                item.remove(i);
                break;
            }
        }
    }

    //结算购物车生成订单
    public void settlement() {
        if (item.size() == 0) {
            System.out.println("抱歉你的购物车中并无商品，请添加购买的商品后再进行结算！");
            return;
        }

        System.out.println("正在结算......请完善你的详细信息");
        Customer per = new Customer();
        per.fillInformation();//完善信息

        //生成订单
        Order or = new Order(per, item.size(), totlePrice);
        or.showOrder();

        Scanner sc = new Scanner(System.in);

        //订单操作
        while (true) {
            System.out.println("是否结算订单:Y/N");
            String s = sc.nextLine();
            if ("Y".equals(s)) {
                or.confirmOrder();
                System.out.println("按任意键继续.......");
                sc.next();
                break;
            } else if ("N".equals(s)) {
                or.confirm();
                System.out.println("取消订单成功");
                break;
            } else {
                System.out.println("请输入正确的操作");
            }
        }
    }
}
